package cn.monkey.transport.netty.server.codec.protocol.tcp.http;

import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import io.netty.handler.codec.http.HttpVersion;

import java.util.function.BiPredicate;

public interface HttpServerProtocolPredicate extends BiPredicate<Channel, ByteBuf> {

    HttpServerProtocolPredicate INSTANCE = new HttpServerProtocolPredicate() {
    };
    @Override
    default boolean test(Channel channel, ByteBuf byteBuf) {
        String s = new String(byteBuf.array());
        String[] split = s.split("\n");
        if (split.length == 0) {
            return false;
        }
        String line0 = split[0];
        String[] line0Arr = line0.split(" ");
        if (line0Arr.length != 3) {
            return false;
        }
        try {
            HttpVersion httpVersion = HttpVersion.valueOf(line0Arr[2]);
            return httpVersion != null;
        } catch (IllegalArgumentException ignore) {
            return false;
        }
    }
}
